package com.lcxsoft.userservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lcxsoft.commonutils.JwtUtils;
import com.lcxsoft.commonutils.R;
import com.lcxsoft.feign.clients.PartnerClient;
import com.lcxsoft.feign.entity.PartnerUser;
import com.lcxsoft.servicebase.exceptionhandler.FoodboxException;
import com.lcxsoft.userservice.entity.Buliding;
import com.lcxsoft.userservice.entity.CustomUser;
import com.lcxsoft.userservice.entity.Dormitory;
import com.lcxsoft.userservice.entity.School;
import com.lcxsoft.userservice.service.BulidingService;
import com.lcxsoft.userservice.service.CustomUserService;
import com.lcxsoft.userservice.service.DormitoryService;
import com.lcxsoft.userservice.service.SchoolService;
import com.lcxsoft.userservice.url.UserServiceURL;
import com.lcxsoft.userservice.vo.BuildingResultVo;
import com.lcxsoft.userservice.vo.BuildingTreeVo;
import com.lcxsoft.userservice.vo.DormitoryChildrenVo;
import com.lcxsoft.userservice.vo.DormitoryResultVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lcx
 * @since 2022-01-16
 */
@RestController
@RequestMapping(UserServiceURL.packageUrl)
public class SchoolController {

    @Resource
    private SchoolService schoolService;

    @Resource
    private BulidingService bulidingService;

    @Resource
    private DormitoryService dormitoryService;

    @Resource
    private CustomUserService customUserService;

    @Resource
    private PartnerClient partnerClient;

    /**
     * 获取学校列表
     * @return
     */
    @RequestMapping(value = UserServiceURL.SchoolUrl.schoolList, method = RequestMethod.POST)
    public R schoolList(){
        List<School> list = schoolService.list(null);
        return R.ok().data("schools", list);
    }

    /**
     * 根据学校名称获取寝室信息
     * @param schoolName
     * @return
     */
    @RequestMapping(value = UserServiceURL.SchoolUrl.queryDormitoryBySchoolName, method = RequestMethod.POST)
    public R queryDormitoryBySchoolName(String schoolName){
        //根据学校名找到学校
        School school = schoolService.getOne(new LambdaQueryWrapper<School>().eq(School::getSchoolName, schoolName));
        List<BuildingResultVo> buildingResultVos = new ArrayList<>();
        if (school == null){
            throw new FoodboxException(20001, "该学校不存在");
        }else {
            //根据学校ID查所有的宿舍楼
            List<Buliding> bulidingList = bulidingService.list(new LambdaQueryWrapper<Buliding>().eq(Buliding::getSchoolId, school.getSchoolId()));
            for (Buliding buliding : bulidingList) {
                BuildingResultVo buildingResultVo = new BuildingResultVo();
                buildingResultVo.setText(buliding.getBulidingName());
                buildingResultVo.setValue(buliding.getBuildingId());
                //根据宿舍楼主键找到宿舍列表
                List<Dormitory> dormitoryList = dormitoryService.list(new LambdaQueryWrapper<Dormitory>().eq(Dormitory::getBulidingId, buliding.getBuildingId()));
                List<DormitoryResultVo> dormitoryResultVos = new ArrayList<>();
                for (Dormitory dormitory : dormitoryList) {
                    DormitoryResultVo dormitoryResultVo = new DormitoryResultVo();
                    dormitoryResultVo.setText(dormitory.getDormitoryName());
                    dormitoryResultVo.setValue(dormitory.getBulidingId());
                    dormitoryResultVos.add(dormitoryResultVo);
                }
                buildingResultVo.setChildren(dormitoryResultVos);
                buildingResultVos.add(buildingResultVo);
            }
        }
        return R.ok().data("dormitorys", buildingResultVos);
    }

    @PostMapping(UserServiceURL.SchoolUrl.querySchoolByUserId)
    public String querySchoolByUserId(@RequestBody String userId){
        CustomUser user = customUserService.getOne(new LambdaQueryWrapper<CustomUser>().eq(CustomUser::getUserId, userId));
        Dormitory dormitory = dormitoryService.getOne(new LambdaQueryWrapper<Dormitory>().eq(Dormitory::getDormitoryId, user.getUserDormitory()));
        Buliding buliding = bulidingService.getOne(new LambdaQueryWrapper<Buliding>().eq(Buliding::getBuildingId, dormitory.getBulidingId()));
        return buliding.getSchoolId();
    }

    /**
     * 根据schoolId查询实体
     * @param schoolId
     * @return
     */
    @PostMapping(UserServiceURL.SchoolUrl.querySchoolBySchoolId)
    public School querySchoolBySchoolId(@RequestBody String schoolId){



        return schoolService.getOne(new LambdaQueryWrapper<School>().eq(School::getSchoolId, schoolId));
    }

    /**
     * 向学校表中添加学校返回学校ID
     * @param schoolName
     * @return
     */
    @PostMapping(UserServiceURL.SchoolUrl.addSchool)
    public String addSchool(@RequestBody String schoolName){
        School school = new School();
        String schoolId = UUID.randomUUID().toString();
        school.setSchoolId(schoolId);
        school.setSchoolName(schoolName);
        school.setCreatetime(LocalDateTime.now());
        boolean save = schoolService.save(school);
        return schoolId;
    }

    /**
     * 根据宿舍号查询学校ID
     * @param dormitoryId
     * @return
     */
    @PostMapping(UserServiceURL.SchoolUrl.querySchoolIdByDormitoryId)
    public String querySchoolIdByDormitoryId(@RequestBody String dormitoryId){
        return schoolService.findSchoolIdByDormitoryId(dormitoryId);
    }

    @PostMapping(UserServiceURL.SchoolUrl.queryBuildingDormitoryByPartnerId)
    public R queryBuildingDormitoryByPartnerId(HttpServletRequest request){
        String partnerId = JwtUtils.getMemberIdByJwtToken(request);
        Map<String, String> params = new HashMap<>();
        params.put("partnerId", partnerId);
        PartnerUser partnerUser = partnerClient.queryPartnerById(params);
        String schoolId = partnerUser.getSchoolId();
        List<Buliding> bulidingList = bulidingService.list(new LambdaQueryWrapper<Buliding>().eq(Buliding::getSchoolId, schoolId));
        List<BuildingTreeVo> buildingTreeVoList = new ArrayList<>();
        for (Buliding buliding : bulidingList) {
            BuildingTreeVo buildingTreeVo = new BuildingTreeVo();
            buildingTreeVo.setValue(buliding.getBuildingId());
            buildingTreeVo.setLabel(buliding.getBulidingName());
            List<Dormitory> dormitoryList = dormitoryService.list(new LambdaQueryWrapper<Dormitory>().eq(Dormitory::getBulidingId, buliding.getBuildingId()));
            List<DormitoryChildrenVo> dormitoryChildrenVoList = new ArrayList<>();
            for (Dormitory dormitory : dormitoryList) {
                DormitoryChildrenVo dormitoryChildrenVo = new DormitoryChildrenVo();
                dormitoryChildrenVo.setLabel(dormitory.getDormitoryName());
                dormitoryChildrenVo.setValue(dormitory.getDormitoryId());
                dormitoryChildrenVoList.add(dormitoryChildrenVo);
            }
            buildingTreeVo.setChildren(dormitoryChildrenVoList);
            buildingTreeVoList.add(buildingTreeVo);
        }
        return R.ok().data("buildingTreeVoList", buildingTreeVoList);
    }

}

